package com.wnhz.work.yz.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.wnhz.work.yz.BaseActivity;
import com.wnhz.work.yz.MyApplication;
import com.wnhz.work.yz.R;
import com.wnhz.work.yz.bean.ApplyMessagebean;
import com.wnhz.work.yz.utils.Url;
import com.wnhz.work.yz.utils.xutils3.MyCallBack;
import com.wnhz.work.yz.utils.xutils3.XUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * ---申请信息
 */
public class ApplyMessageActivity extends BaseActivity implements View.OnClickListener {
    private TextView title;
    public static ApplyMessagebean bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply_message);
        bean = new ApplyMessagebean();


        if (getIntent().getStringExtra("position2") != null) {
            onUpload();
        } else {
            initTitle();
            initView();
        }
    }

    private void onUpload() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        map.put("info_id", getIntent().getStringExtra("position2"));
        showDialog();
        XUtil.Post(Url.INFORMATION_GETINFORMATION, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        Gson gson = new Gson();
                        bean = gson.fromJson(info, ApplyMessagebean.class);
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                initTitle();
                initView();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });
    }

    private void initTitle() {

    }

    private TextView tv_message, tv_people, tv_condition, tv_need;

    private void initView() {
        title = (TextView) findViewById(R.id.view_main_middle_title);
        title.setText("申请信息");
        findViewById(R.id.btn_main_left).setOnClickListener(this);
        findViewById(R.id.rl_message).setOnClickListener(this);//基本信息
        findViewById(R.id.rl_people).setOnClickListener(this);//来访人信息
        findViewById(R.id.rl_condition).setOnClickListener(this);//来访人情况
        findViewById(R.id.rl_need).setOnClickListener(this);//需求
        findViewById(R.id.tv_present).setOnClickListener(this);//提交
        if (getIntent().getStringExtra("look") != null) {
            bean.setCanUpdata("0");
            findViewById(R.id.tv_present).setVisibility(View.GONE);
        } else {
            bean.setCanUpdata("1");
            findViewById(R.id.tv_present).setVisibility(View.VISIBLE);

        }
        //
        tv_message = (TextView) findViewById(R.id.tv_message);
        tv_people = (TextView) findViewById(R.id.tv_people);
        tv_condition = (TextView) findViewById(R.id.tv_condition);
        tv_need = (TextView) findViewById(R.id.tv_need);
        if (getIntent().getStringExtra("position2") != null) {
            tv_message.setText(bean.getBasic_info().getProject_name() + "");
            tv_people.setText("" + bean.getCustom_info().size() + "人");
            int num = 0;
            for (int i = 0; i < bean.getVisit_info().getVisit_district_info().size(); i++) {
                if (bean.getVisit_info().getVisit_district_info().get(i).getStarttime().contains("1970")) {
                    num++;
                } else {
                    bean.getVisit_info().getVisit_district_info().get(i).setShow(true);
                }

            }


            tv_condition.setText("" + (bean.getVisit_info().getVisit_district_info().size() - num) + "个展馆");
            tv_need.setText("已填写");

        }
    }

    public static final int RECORD1 = 123;
    public static final int RECORD2 = 124;
    public static final int RECORD3 = 125;
    public static final int RECORD4 = 126;

    @Override
    public void onClick(View v) {
        Intent intent;
        switch (v.getId()) {
            case R.id.btn_main_left:
                finish();
                break;

            //基本信息
            case R.id.rl_message:
                intent = new Intent(this, BasicMessageActivity.class);
                startActivityForResult(intent, RECORD1);
                break;

            //来访人信息
            case R.id.rl_people:
                intent = new Intent(this, VisitorActivity.class);
                startActivityForResult(intent, RECORD2);
                break;

            //来访情况
            case R.id.rl_condition:
                intent = new Intent(this, VisitorMessage.class);
                startActivityForResult(intent, RECORD3);
                break;

            //需求
            case R.id.rl_need:
                intent = new Intent(this, RequiredActivity.class);
                startActivityForResult(intent, RECORD4);
                break;

            //提交
            case R.id.tv_present:
                upLoadAll();


                break;

            default:
                break;
        }
    }

    private void upLoadAll() {
        Map<String, Object> map = new HashMap<>();
        map.put("token", MyApplication.getInstance().getUserBean().getToken());
        if (tv_message.getText().toString().trim().equals("")) {
            MyToast("请填写基本信息");
            return;
        }
        if (tv_people.getText().toString().trim().equals("")) {
            MyToast("请填写来访人信息");
            return;
        }
        if (tv_condition.getText().toString().trim().equals("")) {
            MyToast("请填写展馆信息");
            return;
        }
        if (tv_need.getText().toString().trim().equals("")) {
            MyToast("请填写需求");
            return;
        }

        ApplyMessagebean bean2 = new ApplyMessagebean();
        bean2.setCustom_info(bean.getCustom_info());
        bean2.setBasic_info(bean.getBasic_info());
        bean2.setNeed_info(bean.getNeed_info());
        //
        ApplyMessagebean.VisitInfoEntity visit_info = bean2.getVisit_info();
        visit_info.setFood_info(bean.getVisit_info().getFood_info());
        visit_info.setHotel_info(bean.getVisit_info().getHotel_info());
        List<ApplyMessagebean.VisitInfoEntity.VisitDistrictInfoEntity> visit_district_info = new ArrayList<>();
        for (int i = 0; i < bean.getVisit_info().getVisit_district_info().size(); i++) {
            if ("开始时间".equals(bean.getVisit_info().getVisit_district_info().get(i).getStarttime()) || "结束时间".equals(bean.getVisit_info().getVisit_district_info().get(i).getEndtime())) {

            } else {
                visit_district_info.add(bean.getVisit_info().getVisit_district_info().get(i));
            }
        }
        visit_info.setVisit_district_info(visit_district_info);
        bean2.setVisit_info(visit_info);
        map.put("all_info", JSON.toJSONString(bean2));

        showDialog();
        Set keys = map.keySet();
        if (keys != null) {
            Iterator iterator = keys.iterator();
            while (iterator.hasNext()) {
                Object key = iterator.next();
                Object value = map.get(key);
                Log.e("----" + key, ":" + value + "\n");
            }
        }
        String url = "";
        if (getIntent().getStringExtra("position2") != null) {
            url = Url.INFORMATION_EDITINFO;
            map.put("info_id", getIntent().getStringExtra("position2"));

        } else {
            url = Url.INFORMATION_ADDINFO;
        }
        XUtil.Post(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("----", "onSuccess:= " + result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("result");
                    String info = jsonObject.optString("info");
                    if ("1".equals(res)) {
                        MyToast(info);
                        finish();
                    } else {
                        if ("-1".equals(res)) {
                            info = "请重新登录";
                        }
                        MyToast(info);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                closeDialog();
                super.onFinished();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                closeDialog();
            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case RECORD1:
                    tv_message.setText(bean.getBasic_info().getProject_name() + "");
                    break;
                case RECORD2:
                    if (bean.getCustom_info().size() == 0) {
                        tv_people.setText("");
                        return;
                    }
                    tv_people.setText("" + bean.getCustom_info().size() + "人");
                    break;
                case RECORD3:
                    int num = 0;
                    for (int i = 0; i < bean.getVisit_info().getVisit_district_info().size(); i++) {
                        Log.e("---", bean.getVisit_info().getVisit_district_info().get(i).isShow() + "");
                        if (bean.getVisit_info().getVisit_district_info().get(i).isShow()) {
                            if (bean.getVisit_info().getVisit_district_info().get(i).getStarttime().contains("开始") || bean.getVisit_info().getVisit_district_info().get(i).getEndtime().contains("结束")) {
                            } else
                                num++;
                        }
                    }
                    if (num == 0) {
                        tv_condition.setText("");
                    } else
                        tv_condition.setText("" + num + "个展馆");
                    break;
                case RECORD4:
                    tv_need.setText("已填写");
                    break;

            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
